package com.tr.rentcar.controller.houtai.car;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.tr.rentcar.model.bean.Cars;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.dao.CarInfoDao;
@WebServlet("/carstore")
public class CarstoreControllor extends HttpServlet {

	private static final long serialVersionUID = 1L;
	CarInfoDao  CarInfoDao=new CarInfoDao();
	Cars Cars=new Cars();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			String cno = req.getParameter("cno");
			List<Cars> Cars =null;
			if(cno==null||cno==""){
				Cars = CarInfoDao.getAll();
			}else{
				String cno2=cno;
				Cars = CarInfoDao.findbycno(cno,cno2);
			}
			 
			req.setAttribute("Cars", Cars);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		req.getRequestDispatcher("/WEB-INF/jsp/houtai_carstore.jsp").forward(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			req.setCharacterEncoding("utf-8");
			resp.setContentType("application/json;charset=utf-8");
			List<Cars> Cars=CarInfoDao.getAll();
			req.setAttribute("Cars", Cars);
			PrintWriter out=resp.getWriter();
			out.print(JSON.toJSONString(Cars));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
